<template>
  <el-form ref="form" :model="form" label-width="120px" style="width:80%" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" placeholder="请选择" style="width:100%">
        <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" type="textarea" placeholder="1-300个字符" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="onSubmit">确定</el-button>
      <el-button size="small" @click="onCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, infoDepartments, updateDepartments } from '@/api/departments'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rules, value, callback) => {
      const codeList = this.originList.map(item => item.code)
      codeList.includes(value) ? callback(new Error('编码' + value + '已占用'))
        : callback()
    }
    const validName = (rules, value, callback) => {
      const nameList = this.originList.map(item => item.name)
      nameList.includes(value) ? callback(new Error('名称' + value + '已存在'))
        : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      list: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.loadEmployeeSimple()
    this.loadDetails()
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.isEdit ? this.onEdit() : this.onAdd()
      })
    },
    async onAdd() {
      try {
        const params = { ...this.form, pid: this.id }
        const res = await addDepartments(params)
        console.log('添加部门', res)
        this.$message({ type: 'success', message: '添加成功' })
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    onCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    },
    async loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        console.log('获取简单员工列表', res.data)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    async loadDetails() {
      if (this.isEdit) {
        const res = await infoDepartments(this.id)
        // console.log('现在是编辑，要去获取详情', res)
        this.form = res.data
      }
    },
    async onEdit() {
      try {
        await updateDepartments(this.form)
        this.$message({ type: 'success', message: '修改成功' })
        this.$emit('success')
      } catch (error) {
        console.log(error)
        this.$message({ type: 'error', message: error })
      }
    }
  }
}
</script>

<style>

</style>
